package cn.tedu.test.rpc.demo02;

import cn.tedu.test.rpc.common.User;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Stub {
    public User findUserById(long id) throws IOException {
        Socket socket=new Socket("127.0.0.1",8000);
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        DataOutputStream dataOutputStream=new DataOutputStream(baos);
        dataOutputStream.writeLong(id);
        socket.getOutputStream().write(baos.toByteArray());
        socket.getOutputStream().flush();

        DataInputStream dataInputStream=new DataInputStream(socket.getInputStream());
        long receivedIdid=dataInputStream.readLong();
        String username=dataInputStream.readUTF();
        User user=new User(id,username);
        System.out.println(user);
        dataOutputStream.close();
        socket.close();
        return user;
    }
}
